<?

/**
* @property DB_active_record $db
* @property Base_flexmodel $base_flexmodel
* */
class Provider extends Base_flexmodel { // CI_Model

	function __construct()
	{
		parent::__construct();
		$this->init('ics_provider_data' , 'pro_id' , 'pro_name');
		//pro_id , is_zone , zone , pro_name , block , street 
		//, building , unit , postal , telp , fax , X , Y
	}
	
	function insert ( 
			$pro_id , $is_zone ,  $zone , $pro_name , $block , $street 
			, $building , $unit , $postal , $telp , $fax , $X , $Y
	){
		$provider = array(
			'pro_id'		=> $pro_id
			, 'is_zone'		=> $is_zone
			, 'zone'		=> $zone
			, 'pro_name'	=> $pro_name
			, 'block'		=> $block
			, 'street'		=> $street
			, 'building'	=> $building
			, 'unit'		=> $unit
			, 'postal'		=> $postal
			, 'telp'		=> $telp
			, 'fax'			=> $fax
			, 'X'			=> $X
			, 'Y'			=> $Y
		);
//		//$this->insert ( $provider );
//		$qq = $this->db->insert_string('ics_provider_data', $provider); 
//		$this->db->query($qq);
		//$this->insert ( $provider );
		parent::insert ( $provider );

	}
	
	function getAll ()
	{
		//$qq = $this->db->get('ics_provider_data');
		//return $this->get ( 'pro_id' , 'asc' );
		return parent::get ( 'pro_name' , 'asc' );
	}

	function getProviderList ( )
	{
		return parent::get_string('pro_name');
	}

	function getProviderList_byAlphabeth( $character )
	{
		$cond = "LEFT(pro_name, 1) = '" . $character . "'" ;
		return parent::get_where ( $cond , 'pro_id , pro_name' );
	}
	function getProviderList_byKeyword( $keyword )
	{
		//$cond = "pro_name = '" . $keyword . "'" ;
		$cond = array ( "pro_name" => $keyword );
		parent::set_order( 'pro_name', 'asc' );
		return parent::get_search( $cond , '' , 'pro_id , pro_name' );
	}
	
	function getProviderDetail( $id )
	{

		parent::set_join( 'ics_services_id'	, 'ics_services_id.pro_id = ics_provider_data.pro_id' );
		parent::set_join( 'ics_services'	, 'ics_services_id.type_id = ics_services.type_id' );
		//return parent::get();
		$cond = array ( "ics_provider_data.pro_id" => $id );
		$provider = parent::get_where( $cond );
		if ( count ( $provider ) > 0  )
			return $provider[0] ;
		else
			return null;
	}
	
	
//	function getByRange($range, $type, $x, $y) {
//		if ($range == "" || !($x > 0 && $y > 0)) return false;
//		
//		$distsqbound = 1000000*$range*$range;		
//		$sql = "SELECT POW(datas.x-?,2)+POW(datas.y-?,2)";		
//		$sql .= "as distsq, datas.*, id.type_id ";
//		if ($type == '') $sql .= ", serv.name as service_name ";
//		$sql .= "from (ics_provider_data datas, ics_services_id id) ";
//		if ($type == '') $sql .= "LEFT JOIN ics_services as serv ON (id.type_id = serv.type_id) ";		
//		$sql .= "WHERE POW(datas.x-?,2)+POW(datas.y-?,2)  <= $distsqbound ";
//		if ($type != '' && strlen($type) > 16) $sql .= "AND id.type_id = ? ";
//
//		$sql .= "AND datas.pro_id = id.pro_id ";
//		$sql .= "ORDER by distsq ";
//		$this->writeLog($sql);
//		$q = ($type != '' && strlen($type) > 16) ? $this->db->query($sql,array($x,$y,$x,$y,$type)) : $this->db->query($sql,array($x,$y,$x,$y));
//		if ($q->num_rows() > 0) {
//			return $q->result();
//		}
//		return false;
//	}
	
//	function getByType($type, $x, $y) {
//		if ($type == "" || !($x > 0 && $y > 0)) return false;
//		$sql = "SELECT serv.name, datas.pro_id, datas.pro_name, datas.x, datas.y, datas.postal, "
//		. " POW(datas.x-?,2)+POW(datas.y-?,2) as distsq "
//		. "	from (ics_services serv, ics_provider_data datas, ics_services_id id) "
//		. " INNER JOIN ( select DISTINCT pro_id from ics_zone where type_id = ? ) Z on datas.pro_id = Z.pro_id "
//		. " where serv.type_id=id.type_id AND datas.pro_id = id.pro_id "
//		. " AND serv.is_zone = 'Y'"
//		. " AND serv.type_id = ?"
//		. " ORDER BY distsq ASC";
//		//. " AND serv.is_zone = 'Y' AND datas.is_zone = 'Y' "		
//		$q = $this->db->query($sql,array($x,$y,$type,$type));
//		if ($q->num_rows() > 0) {
//			return $q->result();
//		}
//		return false;		
//	}
//	
//	function getZone($service, $provNo) {
//		if ($service == "" && $provNo == "") return false;
//		$zTable = "gis_lyr_ics_".trim(strtolower($service));
//		if ($this->db->table_exists($zTable)) {
//			$sql = "select g.OBJECT_VECTOR zone from `$zTable` g, ics_zone z
//			LEFT JOIN ics_provider_data `data` ON (data.pro_id = z.pro_id)
//			where z.type_id = ? AND g.OBJECT_ID = z.object_id AND ";
//			if (is_numeric($provNo)) $sql .= "data.no = ?";
//			else $sql .= "z.pro_id = ?";
//			
//			$q = $this->db->query($sql,array($service,$provNo));
//			
//			if ($q->num_rows() > 0) {
//				return $q->result();
//			}
//		}
//		return false;		
//	}

}

